from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession

from app.database.models import Country, Server


async def get_country(session: AsyncSession, country_id: int) -> Country:

    q = select(Country).where(Country.id == country_id)
    res = await session.execute(q)

    return res.scalar()


async def query_countries(
    session: AsyncSession, proxy_type: str, proxy_privacy: str
) -> list[Country]:

    q = (
        select(Country)
        .join(Country.servers)
        .filter(
            and_(
                Server.status == "active",
                Server.proxy_type == proxy_type,
                Server.proxy_privacy == proxy_privacy,
            )
        )
    )

    res = await session.execute(q)

    return res.scalars().unique().all()
